package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImp implements OrderService {

        /*
        * 预约业务逻辑：
        * 1。检查用户预约的日期，是否设置了可预约
        * 2.检查用户预约当天的人数，是否以约满
        * 3.检查用户是否重复预约
        * 4.检查当前用户是否时会员，是直接预约成功，不是先自动注册会员在注册成功
        * 5.预约成功，修改orderSetting表中可预约人数的值
        * */

        @Autowired
        private OrderDao orderDao;

        @Autowired
        private OrderSettingDao orderSettingDao;

        @Autowired
        private MemberDao memberDao;

    @Override
    public Result order(Map map) throws Exception {
//               1.检查用户预约的日期，是否设置了可预约
        String orderDate = (String) map.get("orderDate");
           OrderSetting orderSetting = orderSettingDao.findByData(DateUtils.parseString2Date(orderDate));

           if(orderSetting ==null){
               return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
           }
//               2.检查用户预约当天的人数，是否以约满
        int number = orderSetting.getNumber();/*可预约人数*/
        int reservations = orderSetting.getReservations();/*已预约人数*/
        if(number==reservations){
            return  new Result(false,MessageConstant.ORDER_FULL);
        }
//               3.检查用户是否重复预约(同一个人，同一天不能 预约同一个体检套餐)
        String telephone = (String) map.get("telephone");
        Integer member_id = memberDao.findMeberId(telephone);
        String setmealId = (String) map.get("setmealId");
        if(member_id != null) {
            Order order = new Order(member_id, DateUtils.parseString2Date(orderDate), Integer.parseInt(setmealId));
            Order ifOrder = orderDao.findByCondition(order);

            if(ifOrder !=null){
//                如果查询的到数据，说明用户已经预约成功过了，不可重复预约
                return new Result(false,MessageConstant.HAS_ORDERED );
            }

        }

        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.modify(orderSetting);
//        创建会员
        String name = (String) map.get("name");
        String sex = (String) map.get("sex");
        String idCard = (String) map.get("idCard");
        Member member = new Member(name,sex,idCard,telephone);
        memberDao.add(member);
        Integer memberId = member.getId();
//               5.预约成功，修改orderSetting表中可预约人数的值（
//                  1.预约成功后，预约表中新增数据
//                  2.修改orderSetting表中的可预约人数，和已预约人数// ）
        String orderType =Order.ORDERTYPE_WEIXIN;
        String orderStatus=Order.ORDERSTATUS_NO;
        Order order = new Order(memberId,DateUtils.parseString2Date(orderDate),orderType,orderStatus,Integer.parseInt(setmealId));
        orderDao.ordering(order);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map =  orderDao.findById(id);
        if(map != null){
            Date orderDate = (Date) map.get("orderDate");
            String date = DateUtils.parseDate2String(orderDate);
            map.put("orderDate",date);
        }
        return map;
    }

}
